package com.sckj.springboot.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;


import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@Component
public class MyHttpSessionListener implements HttpSessionListener {
    private static final Logger logger = LoggerFactory.getLogger(HttpSessionListener.class);
    public Integer count;
    @Override
    public synchronized void sessionCreated(HttpSessionEvent httpSessionEvent){
        logger.info("新用户上线了");
        count++;
        httpSessionEvent.getSession().getServletContext().setAttribute("count",count);
    }

    @Override
    public synchronized void sessionDestroyed(HttpSessionEvent httpSessionEvent){
        logger.info("用户下线");
        count--;
        httpSessionEvent.getSession().getServletContext().setAttribute("count",count);
    }
}
